var cheerio = require('cheerio');
var https = require('https');
var iconv = require('iconv-lite');
var util = require('util');

const station_name_url = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9064';
let url = 'https://kyfw.12306.cn/otn/leftTicket/queryA?leftTicketDTO.train_date=%s&leftTicketDTO.from_station=%s&leftTicketDTO.to_station=%s&purpose_codes=ADULT';


function parse_station_name(train_date, from_station_name, to_station_name) {

    https.get(station_name_url, function (res) {
        var result = '';
        res.on('data', function (chunk) {
            result += chunk;
        });
        res.on('end', function () {
            let from_station = '没有此站';
            let to_station = '没有此站';
            result = result.toString('utf8');
            let l = result.split('@');
            let promise = new Promise(function (resolve, reject) {
                for (let i = 1; i < l.length; i++) {
                    var tmp = l[i].split('|')[1];
    
                    if (tmp === from_station_name) {
                        from_station = l[i].split('|')[2].toUpperCase();
                    }
                    if (tmp === to_station_name) {
                        to_station = l[i].split('|')[2].toUpperCase();
                    }
                }
                var result = {
                    from_station: from_station,
                    to_station: to_station
                }
               
                console.log(result);
                resolve(result);

            });
            
            let search_url = '';
            console.log("fuck");
            promise.then(function(result){
                search_url = util.format(url, train_date, result.from_station , result.to_station);
                console.log(search_url);
                https.get(search_url, function (sres) {
                    let chunks = '';
                    sres.on('data', function (c) {
                        chunks += c;
                    });
                    
                    sres.on('end', function () {
    
                        let train_info = JSON.parse(chunks);
    
                        let train_list = train_info.data.result;
                        let final_result = [];
                        console.log(train_info);
                        train_list.forEach((element, index) => {
                            let list = {};
                            let one_train_info = element.split('|');
                            let start_time = one_train_info[8];
                            let end_time = one_train_info[9];
                            let train = one_train_info[3];
                            let train_no = one_train_info[2];
                            let train_consuming = one_train_info[10];
                            let A9 = one_train_info[32];
                            let A4 = one_train_info[23];
                            let A6 = one_train_info[21];
                            let M = one_train_info[31];
                            let O = one_train_info[30];
                            let P = one_train_info[25];
                            let A3 = one_train_info[28];
                            let A1 = one_train_info[29];
                            let WZ = one_train_info[26];
                            list.start_time = start_time;
                            list.end_time = end_time;
                            list.train = train;
                            list.train_consuming = train_consuming;
                            list.A9 = A9;
                            list.A4 = A4;
                            list.A6 = A6;
                            list.M = M;
                            list.O = O;
                            list.P = P;
                            list.A3 = A3;
                            list.A1 = A1;
                            list.WZ = WZ;
                            final_result.push(list);
                        });
                        ares.send(final_result);
                    });
                });
            });
           
        });

    });

}
parse_station_name('2018-09-06', '上海', '合肥');